package jdbc;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class teacherjudge
 */
@WebServlet("/src/teacherjudge")
public class teacherjudge extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public teacherjudge() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Access-Control-Allow-Origin", "*");//模板
	    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");//模板
	    response.setHeader("Access-Control-Allow-Headers", "Content-Type");//模板
	    //设置编码格式
	    request.setCharacterEncoding("UTF-8");//模板
	    response.setCharacterEncoding("UTF-8");//模板
		
	    String studentid = request.getParameter("studentid");
	    String teacherid = request.getParameter("teacher_id");
	    String flag = request.getParameter("flag");

	    String messageid = request.getParameter("messageid");

	    
	    hello a = new hello();//模板
	    try {
	    	ResultSet b;
	    	b=a.doselect("select message_content from message where message_id= ? ", messageid);
	    	if(b.next()) {
	    		String content=b.getString("message_content");
	    		var s1=content.split(":");
	    		var s2 =s1[1].split("学生id");
	    		String studentname=s2[0].trim();
	    		var s3=s1[3].split("课程id");
	    		String coursename=s3[0].trim();
	    		var s4=s1[4].split("的缓考  理由");
	    		String courseid=s4[0].trim();
	    		String newcontent="我已同意你关于 课程名称:"+coursename+" 课程id: "+courseid+" 的缓考申请";
	    		String newcontent1="我不同意你关于 课程名称:"+coursename+" 课程id: "+courseid+" 的缓考申请";

	    		if(flag.equals("true")) {
	    		a.doupdate("INSERT INTO message (message_object,message_objectid,message_content,message_sender,message_senderid) VALUES ('student', ?, ? , 'teacher', ?) ",studentid,newcontent,teacherid);
			a.doupdate("DELETE FROM message where message_id= ? ",messageid);
	    	}
	    	else {
	    		a.doupdate("INSERT INTO message (message_object,message_objectid,message_content,message_sender,message_senderid) VALUES ('student', ?, ? , 'teacher', ?) ",studentid,newcontent1,teacherid);
				a.doupdate("DELETE FROM message where message_id= ? ",messageid);

	    	}
			response.getWriter().write("true");
	    	}
	    	b.close();

	    }catch (SQLException e) {//模板
			e.printStackTrace();//模板
		}//模板
	}

}
